<?php
/*
 * Created on May 15, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Source_model extends CI_Model
{
	/**
	 * @var unknown_type
	 */
	var $id  ;  
	/**
	 * @var unknown_type
	 */
	var $vi_name  ;
	/**
	 * @var unknown_type
	 */
	var $en_name  ;

        function __construct() {
            parent::__construct();
        }

	function Source_model()
	{
            parent::__construct();
	}
	
	function create($categories){
		if(!is_array($categories))
			$categories = array($categories);
		
		$sql = "INSERT INTO `gtb_pcategory` " .
				"(`id_parent_pcategory`, `code`, `code_parent`, " .
				"`vi_name`, `en_name`, `en_description`, `vi_description`, " .
				"`number_product`, `link_rewrite`, `meta_title`, `meta_keywords`, " .
				"`meta_description`, `level_depth`, `active`, `date_add`, `date_upd`) " .
				"VALUES";
		for($i = 0; $i < count($categories); $i++){
			$category = $categories[$i];
			$sql .= "(" .
					intval($category->id_parent_pcategory).", " .
					 $this->db->escape($category->code).", " .
					 $this->db->escape($category->code_parent).", " .
					 $this->db->escape($category->vi_name).", " .
					 $this->db->escape($category->en_name).", " .
					 $this->db->escape($category->en_description).", " .
					 $this->db->escape($category->vi_description).", " .
					intval($category->number_product).", " .
					 $this->db->escape($category->link_rewrite).", " .
					 $this->db->escape($category->meta_title).", " .
					 $this->db->escape($category->meta_keywords).", " .
					 $this->db->escape($category->meta_description)."," .
					 intval($category->level_depth).", " .
					 intval($category->active).", " .
					 $this->db->escape($category->date_add).", " .
					$this->db->escape($category->date_upd)."" .
					"),";
				
		}
		$sql = substr($sql, 0, strlen($sql)-1);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
		
	}
	
	function insert(){
				
		$sql = "INSERT INTO `gtb_source` " .
				"(" .
				"`vi_name`, `en_name`)" .
				" VALUES ";
		
		$sql .= "(" .
				$this->db->escape($this->vi_name).", " .
				$this->db->escape($this->en_name).
				")";
			
		$this->db->query($sql);
		return $this->db->insert_id();
		
	}
	
	function update($source){
		 $sql = "UPDATE gtb_source 
				 SET ".
				" vi_name = " .  $this->db->escape($source['vi_name']) . "," .
				" en_name = " .  $this->db->escape($source['en_name']) .
				" WHERE id = " .intval($source['id']);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function delete($id){
		// Kiem tra trc khi xoa, neu co record tham chieu thi ko cho xoa.
		// .......... chua lam
		
		if(!is_array($id)) $id = array($id);
		$ids = implode(',', $id);
		$sql = "DELETE FROM gtb_source WHERE id IN ($ids)";
		//$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function getAll(){
		$result = null;
		$sql = "SELECT s.* " .
				" FROM gtb_source s " .
				" ORDER BY s.id DESC";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getAllCount(){
		$result = null;
		$sql = "SELECT COUNT(*) as num_record " .
				" FROM gtb_source " ;
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		if($result) return $result[0]->num_record;
		
		return 0;
	}
	
	function getByParentCode($code_parent){
		$result = null;
		$sql = "SELECT *
				FROM gtb_pcategory WHERE code_parent_pcategory = '".$this->db->escape($code_parent)."' ORDER BY id_pcategory DESC";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getMaxPosition($category_parent_id){
		
		$sql = "SELECT MAX(position) as position_max
				FROM category 
				WHERE category_parent_id = $category_parent_id";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}
       
		return 0;
	}
	
	function getById($category_id){
		
		$sql = "SELECT *
				FROM gtb_source 
				WHERE id = " . intval($category_id);
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0];
		}
       
		return null;
	}
	
	function getByCode($code){
		
		$sql = "SELECT *
				FROM gtb_pcategory 
				WHERE code = " . $this->db->escape($code);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	function getByLinkRewrite($link_rewrite){
		
		$sql = "SELECT *
				FROM gtb_pcategory 
				WHERE link_rewrite = " . $this->db->escape($link_rewrite);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	function isInGroup($user_id, $group_id){
		$sql = "SELECT 1 as is_member FROM user_group ug
				INNER JOIN users u ON u.id = ug.user_id
				WHERE group_id = $group_id AND ug.user_id = $user_id";
		$query = $this->db->query($sql);
		if($query->result())
			return 1;
		
		return 0;
		
	}
	
	//function getUserBy
	
//	
}

// EOF

